package com.topn2;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class TReducer extends Reducer<Tkey, IntWritable, Text, IntWritable> {

    private Text rkey = new Text();
    private IntWritable rval = new IntWritable();

    @Override
    protected void reduce(Tkey key, Iterable<IntWritable> values, Reducer<Tkey, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        Iterator<IntWritable> iter = values.iterator();

        // 是否是该组的第一条
        boolean isFirst = true;
        int day = 0;
        while (iter.hasNext()) {
            iter.next();
            if (isFirst) {
                rkey.set(key.toString());
                rval.set(key.getTemp());
                context.write(rkey, rval);
                day = key.getDay();
                isFirst = false;
            } else {
                int currentDay = key.getDay();
                if(day != currentDay) {
                    rkey.set(key.toString());
                    rval.set(key.getTemp());
                    context.write(rkey, rval);
                    break;
                }
            }

        }
    }
}
